﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;

namespace Yuanlin.Business
{
    public class CompanyFriendEditControl : YuanlinUserControl,
        IReplyConverter<CompanyFriendReply, GetOperation>
    {
        protected ValuedTextBox friendGreenNumberValuedTextBox;
        protected ValuedDropDownList categoryDropDownList;

        protected IButton SaveButton;
        protected IButton BackButton;
        private CompanyFriend companyFriend;
        private YuanlinUser loginUser = HttpContext.Current.User as YuanlinUser;
        private int companyFriendId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CompanyFriendId);

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            requestBinders.Add(RequestHelper.BuildGetCompanyFriendRequest(this));
        }
        protected override void OnLoad(EventArgs e)
        {
            SaveButton.Click += new EventHandler(SaveButton_Click);
            BackButton.Click += new EventHandler(BackButton_Click);
        }

        private void Bind()
        {
            if (companyFriend != null)
            {
                this.friendGreenNumberValuedTextBox.Value = companyFriend.FriendGreenNumber.Value;
                BindDDL();
                categoryDropDownList.Value = companyFriend.CategoryId.Value.ToString();
            }   
        }
        private void BindDDL()
        {
            CompanyFriendCategoryListRequest request = new CompanyFriendCategoryListRequest();
            request.UserId = loginUser.EntityId.Value;
            request.PageIndex = 0;
            request.PageSize = 1000;
            EntityList categories = BusinessManager.GetCompanyFriendCategoryList(RequestHelper.BuildGetCompanyFriendCategoryListRequest(request));
            categoryDropDownList.DataSource = categories;
            categoryDropDownList.DataValueField = "EntityId";
            categoryDropDownList.DataTextField = "Title";
            categoryDropDownList.DataBind();
        }

        public void BackButton_Click(object sender, EventArgs e)
        {
            PageRedirect();
        }

        public void SaveButton_Click(object sender, EventArgs e)
        {
            if (companyFriend != null)
            {
                CompanyGreenNumListRequest request = new CompanyGreenNumListRequest();
                request.GreenNum = this.friendGreenNumberValuedTextBox.Value;
                EntityList companyGreenNums = BusinessManager.GetCompanyGreenNumList(RequestHelper.BuildGetCompanyGreenNumListRequest(request));
                if (companyGreenNums.Count == 0)
                {
                    Globals.ShowMessage(this.Page, "CompanyFriendAdd", "当前绿商号不存在，请输入正确的绿商号！");
                    return;
                }

                CompanyGreenNum companyGreenNum = companyGreenNums[0] as CompanyGreenNum;
                YuanlinUser user = UserManager.GetUser(companyGreenNum.UserId.Value) as YuanlinUser;
                companyFriend.FriendUserId.Value = user.EntityId.Value;

                companyFriend.FriendGreenNumber.Value = CensorWordManager.CensorWord(this.friendGreenNumberValuedTextBox.Value);
                companyFriend.CategoryId.Value = int.Parse(categoryDropDownList.Value);

                if (BusinessManager.UpdateCompanyFriend(companyFriend) == -2)
                {
                    Globals.ShowMessage(this.Page, "修改失败", "此信息已经存在！");
                    return;
                }
            }
            PageRedirect();
        }
        private void PageRedirect()
        {
            Page.Response.Redirect(SiteUrls.Instance().GetCompanyFriendListUrl());
        }
  
        #region IReplyConverter<CompanyFriendReply,GetOperation> Members

        public void ConvertReply(CompanyFriendReply reply, GetOperation operation)
        {
            if (reply != null)
            {
                companyFriend = reply.Entity as CompanyFriend;

                if (!Page.IsPostBack)
                {
                    Bind();
                }
            }
        }

        #endregion
    }
}